/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2024-12-28     金鹏       the first version
 */
#include "esp8266.h"

static rt_thread_t wifi_handl = RT_NULL;                            /* wifi线程句柄 */
static struct at_client *wifi_client = RT_NULL;                     /* wifi客户端句柄*/
static at_response_t wifi_resp = RT_NULL;                           /* wifi定时器响应句柄*/

extern rt_mailbox_t wifi_mailbox;                                   /* wifi邮箱句柄*/
extern rt_mq_t wifi_recv_mq;                                        /* wifi接收消息队列*/
extern int cm_switch;                                               /* 0:WIFI; 1:4G*/

/*
 * AT command
 * @Param command
 *
 * Change Logs:
 * Date        Author Notes
 * 2025-1-5     金鹏         AT指令
 */
int WIFI_ATCMD(char *command)
{
    if(at_obj_exec_cmd(wifi_client,at_resp_set_info(wifi_resp, 256, 0, 20 * RT_TICK_PER_SECOND),command) == RT_EOK){
        return RT_EOK;
    }
    else {
        return -RT_ERROR;
    }
}

/*
 * WIFI_Client_Init
 * @Param NULL
 *
 * Change Logs:
 * Date        Author Notes
 * 2025-1-11     金鹏   WIFI客户端初始化
 */
static ENTRY WIFI_Client_Init(void)
{
//    char str[50] = "";

    /* 等待接收wifi客户端句柄*/
    if(rt_mb_recv(wifi_mailbox, (rt_uint32_t *)&wifi_client, RT_WAITING_FOREVER) == RT_EOK){
        rt_kprintf("wifi client = %d\n",wifi_client);
    }else{
        return -RT_ERROR;
    }

    /* 等待接收wifi定时器响应句柄*/
    if(rt_mb_recv(wifi_mailbox, (rt_uint32_t *)&wifi_resp, RT_WAITING_FOREVER) == RT_EOK){
        rt_kprintf("wifi resp = %d\n",wifi_resp);
    }else{
        return -RT_ERROR;
    }

    /* 发送客户端信息*/
    if(WIFI_ATCMD("AT+MQTTUSERCFG=0,1,\"stm32f4\",\"stm32f4\",\"5929735\",0,0,\"\"") != RT_EOK){
        rt_kprintf("MQTT login failed\n");
        return -RT_ERROR;
    }else{
        rt_kprintf("MQTT login successed\n");
    }

    /* 发送连接信息*/
    if(WIFI_ATCMD("AT+MQTTCONN=0,\"broker.emqx.io\",1883,1") != RT_EOK){
        rt_kprintf("MQTT connect failed\n");
        return -RT_ERROR;
    }else{
        rt_kprintf("MQTT connect successed\n");
    }

    /* 确认MQTT信息*/
    WIFI_ATCMD("AT+MQTTCONN?");

    /* 订阅主题*/
    if(WIFI_ATCMD("AT+MQTTSUB=0,\"jp-topic\",1") != RT_EOK){
        rt_kprintf("Sub failed\n");
        return -RT_ERROR;
    }else{
        rt_kprintf("Sub successed\n");
    }

    /* 对话主题*/
    if(WIFI_ATCMD("AT+MQTTSUB=0,\"jp-com2\",1") != RT_EOK){
        rt_kprintf("Sub failed\n");
        return -RT_ERROR;
    }else{
        rt_kprintf("Sub successed\n");
    }

    /* 向[jp-topic]发送[hello]*/
    if(WIFI_ATCMD("AT+MQTTPUB=0,\"jp-topic\",\"你好\",1,0") != RT_EOK){
        rt_kprintf("Sent failed\n");
        return -RT_ERROR;
    }else{
        rt_kprintf("Sent successed\n");
    }

//    Json_Add(str, "gps", 253);
//    Json_Sent("jp-topic", str);

    /* 销毁WIFI邮箱*/
    rt_mb_delete(wifi_mailbox);

    /* 释放响应定时器*/
//    if (wifi_resp){
//        at_delete_resp(wifi_resp);
//    }
    return RT_EOK;
}


/*
 * WIFI_Recv_Init
 * @Param NULL
 *
 * Change Logs:
 * Date        Author Notes
 * 2025-1-11     金鹏   WIFI接收初始化
 */
static ENTRY WIFI_Recv_Init(void)
{
    /* 创建wifi接收消息队列*/
    wifi_recv_mq = rt_mq_create("wifi_recv",
                              MQ_STACK_SIZE,
                                   MQ_COUNT,
                                    MQ_FLAG);

    if(wifi_recv_mq == RT_NULL){
        rt_kprintf("wifi recv mq create failed\n");
        return -RT_ERROR;
    }else{
        rt_kprintf("wifi recv mq create successed\n");
    }

    return RT_EOK;
}

/*
 * WIFI_Entry
 * @Param NULL
 *
 * Change Logs:
 * Date           Author Notes
 * 2024-12-28     金鹏         wifi线程
 */
void WIFI_Entry(void *parameter)
{
    char buff[50] = "";
    char *ret;
    while(1){
        /* wifi接收命令*/
        if(rt_mq_recv(wifi_recv_mq, buff, sizeof(buff), MQ_WAIT_FLAG) == RT_EOK){
            if(cm_switch == 0){
                ret = rt_strstr(buff,"@");
                /* 指令处理*/
                command_deal(ret);
            }
            else{
                rt_memset(buff, 0, sizeof(buff));
            }
        }
    }
}

/*
 * ATK_8266_Test
 * @Param NULL
 *
 * Change Logs:
 * Date           Author Notes
 * 2024-12-28     金鹏         wifi测试
 */
ENTRY ATK_8266_Test(void)
{
    /* WIFI客户端初始化*/
    if(WIFI_Client_Init() != RT_EOK){
        rt_kprintf("WIFI_Client_Init failed\n");
        return -RT_ERROR;
    }else{
        rt_kprintf("WIFI_Client_Init Successful\n");
    }

    /* WIFI接收初始化*/
    if(WIFI_Recv_Init() != RT_EOK){
        rt_kprintf("WIFI_Recv_Init failed\n");
        return -RT_ERROR;
    }else{
        rt_kprintf("WIFI_Recv_Init Successful\n");
    }

    /* 创建wifi线程*/
    wifi_handl = rt_thread_create("wifi",
                                   WIFI_Entry,RT_NULL,
                                   ENTRY_STACK_SIZE-512,
                                   ENTRY_PRI,
                                   ENTRY_TICK);

    /* LOG*/
    if(wifi_handl != RT_NULL){
        rt_kprintf("wifi thread create successful!!\n");

        /* 启动wifi线程*/
        rt_thread_startup(wifi_handl);
    }else{
        rt_kprintf("wifi thread create failed!!\n");

        return -RT_ERROR;
    }

    return RT_EOK;
}
